package br.com.sgi.bean;

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;

public class ClassePDF {


	public static void main(String[] args) throws DocumentException {
	        // criação do objeto documento
	       Document document = new Document();
	       try {
	           
	    	   PdfReader reader = new PdfReader("C://Users/lucas.costa/Documents/DATASUS/Arquivos/2ª Via de Recibo.pdf");
	    	   byte[] streamBytes = reader.getPageContent(1);
	    	   StringBuffer buf = new StringBuffer();
	    	   String contentStream = new String(streamBytes);
	    	   String searchString = "Via";
	    	   int pos = contentStream.indexOf(searchString);
	    	   buf.append(contentStream.substring(0, pos));
	    	   buf.append("XXXX");
	    	   buf.append(contentStream.substring(pos + searchString.length() + 1 ));
	    	   String modifiedString = buf.toString();
	    	   PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("C://Users/lucas.costa/Documents/DATASUS/Arquivos/PDF_LinhaCodigo.pdf"));
	    	   reader.setPageContent(1, modifiedString.getBytes()); 
	    	   
	           PdfWriter.getInstance(document, new FileOutputStream("C://Users/lucas.costa/Documents/DATASUS/Arquivos/PDF_LinhaCodigo2.pdf"));
	           document.open();
	           
	           // adicionando um parágrafo ao documento
	           document.add(new Paragraph(modifiedString));
	         //  document.add(new Paragraph("Exemplo Geração de Arquivo PDF via iText - Java"));
}
//	       catch(DocumentException de) {
//	           System.err.println(de.getMessage());
//	       }
	       catch(IOException ioe) {
	           System.err.println(ioe.getMessage());
	       }
	       document.close();
	   }



}
